package com.zhiling.www.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhiling.www.entity.PageBean;
import com.zhiling.www.entity.Result;
import com.zhiling.www.entity.Stock;
import com.zhiling.www.mapper.StockMapper;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class StockServiceImpl implements StockService{


    private Logger logger = Logger.getLogger(StockServiceImpl.class);
    @Autowired
    private StockMapper stockMapper;

    //分页查询所有股票信息
    @Override
    public Result findStockList(int page, int pageSize) {
        Result result = new Result();
        PageHelper.startPage(page,pageSize);
        List <Stock> stocks = stockMapper.selectAll();
        Page<Stock> plist = (Page <Stock>) stocks;
        PageBean<Stock> pb = new PageBean <>(
                plist.getPageNum(),
                plist.getTotal(),
                plist.getPageSize(),
                plist.getPages(),
                plist.getResult()
        );
        result.setCode(200);
        result.setData(pb);
        return result;
    }

    //查询单个股票信息
    @Override
    public Result searchStock(String codeOrName) {
        Result result = new Result();
        Example example = new Example(Stock.class);
        Example.Criteria criteria = example.createCriteria();
        //根据股票代码或者股票名称查找
        criteria.orEqualTo("stockCode",codeOrName);
        criteria.orEqualTo("stockName",codeOrName);
        Stock stock = stockMapper.selectOneByExample(example);
        if (stock == null){
            result.setCode(400);
            result.setMsg("没有找到该股票");
            return result;
        }
        //logger.debug(stock);
        result.setCode(200);
        result.setData(stock);
        return result;
    }

}
